<template>
    <div>
        <div class="formContainer">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm jform">
                <el-form-item label="书名" prop="name" label-width="120px">
                    <el-input v-model="ruleForm.name" style="width: 217px;"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">立即查询</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>


        </div>
        <el-divider></el-divider>
        <div class="tableContainer">
            <el-table :data="queryResult" stripe size="medium">
                <el-table-column prop="bkId" label="编号"></el-table-column>
                <el-table-column prop="bkName" label="书名"></el-table-column>
                <el-table-column prop="bkAuthor" label="作者"></el-table-column>
                <el-table-column prop="bkPress" label="出版社"></el-table-column>
                <el-table-column prop="bkDatePress" label="出版时间"></el-table-column>
                <el-table-column prop="bkISBN" label="ISBN"></el-table-column>
                <el-table-column prop="bkCatalog" label="分类"></el-table-column>
                <el-table-column prop="bkLanguage" label="语言"></el-table-column>
                <el-table-column prop="bkPages" label="页数"></el-table-column>
                <el-table-column prop="bkPrice" label="价格"></el-table-column>
                <el-table-column prop="bkDateIn" label="入馆时间"></el-table-column>
                <el-table-column prop="bkStatus" label="状态"></el-table-column>
                <el-table-column prop="bkBrief" label="书籍简介"></el-table-column>
                <el-table-column
                        fixed="right"
                        label="操作"
                        width="100">
                    <template slot-scope="scope">
                        <el-button @click="handleClickDelete()" type="text" size="small">删除</el-button>
                        <el-button @click="handleClickUpdate()" type="text" size="small">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-divider></el-divider>
        <el-row :gutter="24">
            <el-col span="2" offset="10">
                <el-button type="primary" @click="gotoApply" v-show="show">办理</el-button>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import {deleteBook, queryBook} from "@/api";
    import {mapState} from 'vuex'

    export default {
        name: "",
        data() {
            return {
                searchParams: {},
                ruleForm: {
                    id:0,
                    name: '',
                    BkNO: '',
                    author: '',
                    press: '',
                    date: '',
                    language: '',
                    pageNum: '',
                    price: '',
                    ISBN: '',
                    category: '',
                    content: '',
                    qty: '',
                    enterDate:'',
                    status: ''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入书名', trigger: 'blur'},
                        {min:1, max: 20, message: '长度在 1 到 20个字符', trigger: 'blur'}
                    ]
                }
            };
        },
        computed: {
            ...mapState({
                queryResult: (state) => state.Book.bookList
            })
        },
        activated() {
            this.$store.dispatch("clear")
        }
        ,
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.searchParams=this.ruleForm
                        this.$store.commit("CLEAR")
                        this.$store.dispatch("queryBook", this.searchParams)
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            handleClickUpdate(){
               this.$router.replace("/book/updateBook/edit")
            },
            handleClickDelete(){
                this.searchParams={}
                this.searchParams.id=this.queryResult[0].bkId
                this.deleteBookFn(this.searchParams)
            },
            async deleteBookFn( params = {}) {
                let response = await deleteBook(params);
                if (response.code == 200) {
                    alert("删除成功")
                    this.$store.dispatch("queryBook",params)

                }
            },
        }

    }
</script>

<style scoped>
    .formContainer {
        width: 500px;
        margin: 0 auto;
    }

    .tableContainer {
        position: relative;
        right: 0px;
        left: 0px;
    }
</style>
